
<template>
  <div class="dashboard-container">
    <el-drawer ref="drawer" size="600px" :title="status==1?'新增':'编辑'" :visible.sync="showDialog" :wrapper-closable="true" @close="close">
      <div class="detial">
        <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="120px" class="demo-ruleForm">
          <el-form-item label="用户:" prop="uid">
            <el-select
              v-model="ruleForm.uid"
              style="width:100%"
              placeholder="请选择用户"
              filterable
            >
              <el-option
                v-for="(item,index) in UserList"
                :key="index"
                :label="item.username"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="渠道:" prop="channel_id">
            <el-select
              v-model="ruleForm.channel_id"
              style="width:100%"
              placeholder="请选择渠道"
            >
              <el-option
                v-for="(item,index) in ChannelList"
                :key="index"
                :label="item.name "
                :value="item.id"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="价格模版:" prop="freight_tem_id">
            <el-select
              v-model="ruleForm.freight_tem_id"
              style="width:100%"
              placeholder="请选择价格模版"
              filterable
            >
              <el-option
                v-for="(item,index) in PriceList"
                :key="index"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
        </el-form>
      </div>
      <div class="buttons">
        <el-button
          v-if="status == 1"
          size="mini"
          type="primary"
          style="margin-left:20px"
          @click="addNews()"
        >
          新 增
        </el-button>
        <el-button
          v-if="status == 2"
          size="mini"
          type="primary"
          style="margin-left:20px"
          @click="editNews()"
        >
          保 存
        </el-button>
        <el-button
          size="mini"
          type=""
          @click="close()"
        >
          取消
        </el-button>

      </div>

    </el-drawer>
  </div>
</template>

<script>
import { createChannelEmpower, updateChannelEmpower, listChannel } from '@/api/vocational/irrigation'
import { listUser } from '@/api/user/index'
import { listFreight } from '@/api/vocational/price'

export default {
  name: 'Detial',
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    detial: {
      require: false,
      type: Object,
      default: () => {

      }
    },
    status: {
      type: Number,
      default: 1
    }
  },
  data() {
    return {
      time: '',
      showDialog: false,
      ChannelList: [],
      PriceList: [],
      UserList: [],
      ruleForm: {
        uid: '',
        channel_id: '',
        freight_tem_id: ''
      },
      rules: {
        uid: [
          { required: true, message: '请选择用户' }
        ],
        channel_id: [
          { required: true, message: '请选择渠道' }
        ],
        freight_tem_id: [
          { required: true, message: '请选择价格模版' }
        ]
      }
    }
  },
  watch: {
    visible(val) {
      this.showDialog = this.visible
      if (!this.showDialog) {
        this.resetForm()
      } else {
        if (this.status === 2) {
          Object.keys(this.ruleForm).forEach(i => {
            this.ruleForm[i] = this.detial[i]
          })
          this.ruleForm.id = this.detial.id
        }
      }
    }
  },
  mounted() {
    this.getUserList()
    this.getChannelList()
    this.getPriceList()
  },
  methods: {
    getUserList() {
      listUser({
        limit: 100000
      }).then(res => {
        this.UserList = res.data.data
      })
    },
    getChannelList() {
      listChannel({
        limit: 100000,
        status: 1
      }).then(res => {
        this.ChannelList = res.data.data
      })
    },
    getPriceList() {
      listFreight({
        limit: 100000,
        status: 1
      }).then(res => {
        this.PriceList = res.data.data
      })
    },

    close() {
      this.$emit('update', false)
      this.ruleForm = {
        uid: '',
        channel_id: '',
        freight_tem_id: ''
      }
    },
    resetForm() {
      this.$refs['ruleForm'].resetFields()
    },
    addNews() {
      this.$refs['ruleForm'].validate((valid) => {
        if (valid) {
          createChannelEmpower(this.ruleForm).then(res => {
            this.$message.success('添加成功')
            this.$emit('reload')
          })
        } else {
          return false
        }
      })
    },
    editNews() {
      this.$refs['ruleForm'].validate((valid) => {
        if (valid) {
          updateChannelEmpower(this.ruleForm).then(res => {
            this.$message.success('编辑成功')
            this.$emit('reload')
          })
        } else {
          return false
        }
      })
    }

  }

}
</script>
<style scoped>
.detial{
  padding:40px 40px 20px 20px;
}
.buttons{
  display: flex;
  flex-direction: row-reverse;
  padding-right: 40px;
}
</style>
